﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SQLite;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Words
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            Connection();
        }

        List<string> cnList = new List<string>();
        List<string> enList = new List<string>();
        int m = 0;//cnList索引
        int n = 0;//enList索引
        bool flag = true;//是否已经测试完毕，初始为true表示还未开始测试

        private void Connection()
        {
            //数据库文件使用SQLiteExpertPro创建
            string StartupPath = Application.StartupPath;
            StartupPath += "\\words.db";
            // 连接数据库    
            try
            {
                string connectionString = "data source = " + StartupPath;
                SQLiteConnection dbConnection = new SQLiteConnection(connectionString);
                if (dbConnection.State != System.Data.ConnectionState.Open)
                {
                    dbConnection.Open();
                    SQLiteCommand cmd = new SQLiteCommand();
                    cmd.Connection = dbConnection;
                    cmd.CommandText = "SELECT * FROM learn";
                    cmd.ExecuteNonQuery();//执行
                    SQLiteDataReader sr = cmd.ExecuteReader();
                    while (sr.Read())
                    {
                        cnList.Add(sr.GetString(0).ToString());
                        enList.Add(sr.GetString(1).ToString());

                    }
                    sr.Close();
                    
                }
                dbConnection.Close();
            }catch(Exception a)
            {
                MessageBox.Show(a.Message);
            }
            
        }


        private void button1_Click(object sender, EventArgs e)
        {
            myClear();
            //显示第一个中文词义
            textCn.Text += cnList[m];
        }

        private void myClear()
        {
            m = n = 0;
            textCn.Text = textEn.Text = "";
            flag = false;
        }

        private void Form1_KeyDown(object sender, KeyEventArgs e)
        {
            try
            {
                if (e.KeyCode == Keys.Enter)
                {
                    if (flag == false)//如果还未测试完毕则执行
                    {
                        if (textEn.Text == "")
                        {
                            MessageBox.Show("还未输入英文单词！");
                        }
                        else
                        {
                            //判断输入的英文是否正确
                            myAssentment();
                            //继续下一行的单词
                            m += 1;
                            n += 1;
                            if (m <= cnList.Count() - 1)
                            {
                                textCn.Text = cnList[m];
                                textEn.Text = "";
                            }
                            else
                            {
                                MessageBox.Show("已经测试完毕!");
                                flag = true;
                            }
                        }
                        
                    }
                    else//如果已经测试完毕或者还未开始
                    {
                        MessageBox.Show("已经测试完毕或者还未开始测试!");
                    }
                    
                    
                }
            }catch(Exception a)
            {
                MessageBox.Show(a.Message);
            }
            
        }

        private void myAssentment()
        {
            if(textEn.Text.ToLower() == enList[n])
            {
                MessageBox.Show("正确！");
            }
            else
            {
                MessageBox.Show("错误！");
            }
        }

        
    }
}
